<?php
/**
 *
 * 版权所有：忠赢网站管理系统<www.91zoy.com>

 * 日    期：2016-09-20
 * 版    本：1.0.0
 * 功能说明：文章控制器。
 *
 **/

namespace Qwadmin\Controller;

use Vendor\Tree;

class ProductController extends ComController
{

    public function add()
    {

        $pcategory = M('pcategory')->field('id,title')->order('o asc')->select();
//        $tree = new Tree($pcategory);
//        $str = "<option value=\$id \$selected>\$spacer\$name</option>"; //生成的形式
//        $pcategory = $tree->get_tree(0, $str, 0);
//        pp($pcategory);exit();
        $this->assign('pcategory', $pcategory);//导航
        $this->display('form');
    }

    public function index($cid = 0, $p = 1)
    {
//        pp($_GET);exit();
        $p = intval($p) > 0 ? $p : 1;
        $product = M('product');
        $pagesize = 20;#每页数量
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        $prefix = C('DB_PREFIX');
        $cid = isset($_GET['cid']) ? $_GET['cid'] : '';
        $keyword = isset($_GET['keyword']) ? htmlentities($_GET['keyword']) : '';
        $order = isset($_GET['order']) ? $_GET['order'] : 'DESC';
        $where = '1 = 1 ';
        if ($cid) {
            $cids_array = pcategory_get_sons($cid);
            $cids = implode(',',$cids_array);
            $where .= "and {$prefix}product.cid in ($cids) ";
        }
        if ($keyword) {
            $where .= "and {$prefix}product.title like '%{$keyword}%' ";
        }
        //默认按照时间降序
        $orderby = "ctime desc";
        if ($order == "asc") {
            $orderby = "ctime asc";
        }
        //获取栏目分类
        $pcategory = M('pcategory')->field('id,title')->order('o asc')->select();
        $tree = new Tree($pcategory);
        $str = "<option value=\$id \$selected>\$spacer\$name</option>"; //生成的形式
        $pcategory = $tree->get_tree(0, $str, $cid);
        $this->assign('pcategory', $pcategory);//导航


        $count = $product->where($where)->count();
        $list = $product->field("{$prefix}product.*,{$prefix}pcategory.title as cate_name")->where($where)->order($orderby)->join("{$prefix}pcategory ON {$prefix}pcategory.id = {$prefix}product.cid")->limit($offset . ',' . $pagesize)->select();
//        pp($list);exit();
        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();
        $this->assign('list', $list);
        $this->assign('page', $page);
        $this->display();
    }

    public function del()
    {

        $product_ids = isset($_REQUEST['product_ids']) ? $_REQUEST['product_ids'] : false;
        if ($product_ids) {
            if (is_array($product_ids)) {
                $product_ids = implode(',', $product_ids);
                $map['product_id'] = array('in', $product_ids);
            } else {
                $map = 'product_id=' . $product_ids;
            }
            if (M('product')->where($map)->delete()) {
                addlog('删除文章，AID：' . $product_ids);
                $this->success('恭喜，文章删除成功！');
            } else {
                $this->error('参数错误！');
            }
        } else {
            $this->error('参数错误！');
        }
    }

    public function edit($product_id)
    {

        $product_id = intval($product_id);
        $product = M('product')->where('product_id=' . $product_id)->find();
        if ($product) {

            $pcategory = M('pcategory')->field('id,title')->order('o asc')->select();
            $this->assign('pcategory', $pcategory);//导航

            $this->assign('product', $product);
        } else {
            $this->error('参数错误！');
        }
        $this->display('form');
    }

    public function update($product_id = 0)
    {
        $product_id = intval($product_id);
        $data['cid'] = isset($_POST['cid']) ? intval($_POST['cid']) : 0;
        $data['title'] = isset($_POST['title']) ? $_POST['title'] : false;
        $data['xinghao'] = isset($_POST['xinghao']) ? $_POST['xinghao'] : '';
        $data['guige'] = isset($_POST['guige']) ? $_POST['guige'] : '';
        $data['yongtu'] = isset($_POST['yongtu']) ? $_POST['yongtu'] : '';
        $data['content'] = isset($_POST['content']) ? $_POST['content'] : false;
        $thumbnail = I('post.thumbnail', '', 'strip_tags');
        $data['xingzhuang'] = isset($_POST['xingzhuang']) ? $_POST['xingzhuang'] : 0;
        $data['ctime'] =date("Y-m-d H:i:s",time());
        if (!$data['cid'] or !$data['title'] or !$data['content']) {
            $this->error('警告！文章分类、文章标题及文章内容为必填项目。');
        }
        if ($product_id) {
            M('product')->data($data)->where('product_id=' . $product_id)->save();
            if(!empty($thumbnail)){
                M("photos")->where(array('item_id'=>$product_id))->delete();
                $thumbnail_arr = explode("|",$thumbnail);
                foreach ($thumbnail_arr as $key=>$val){
                    M("photos")->add(array(
                        'image_url'=>$val,
                        'item_id'=>$product_id,
                        'type_id'=>1,
                        'ctime'=>date("Y-m-d H:i:s",time()),
                    ));
                }
            }
            M('product')->data($data)->where('product_id=' . $product_id)->save();
            addlog('编辑文章，AID：' . $product_id);
            $this->success('恭喜！文章编辑成功！',U('/Qwadmin/Product/index'),2);
        } else {
            $data['t'] = date("Y-m-d H:i:s");
            $product_id = M('product')->data($data)->add();
            if ($product_id) {
                if(!empty($thumbnail)){
                    $thumbnail_arr = explode("|",$thumbnail);
                    foreach ($thumbnail_arr as $key=>$val){
                        M("photos")->add(array(
                            'image_url'=>$val,
                            'item_id'=>$product_id,
                            'type_id'=>1,
                            'ctime'=>date("Y-m-d H:i:s",time()),
                        ));
                    }
                }
                addlog('新增文章，AID：' . $product_id);
                $this->success('恭喜！文章新增成功！',U('/Qwadmin/Product/index'),2);
            } else {
                $this->error('抱歉，未知错误！');
            }

        }
    }

    public function change()
    {

        $product_id = isset($_REQUEST['product_id']) ? $_REQUEST['product_id'] : false;
        $type = isset($_REQUEST['type']) ? $_REQUEST['type'] : false;
        if ($product_id) {
            $map = 'product_id=' . $product_id;
            if($type == 1){
                $data = array('status'=>1);
            }else{
                $data = array('status'=>0);
            }
            if (M('product')->data($data)->where($map)->save()) {
                $this->success('恭喜，操作成功！');
            } else {
                $this->error('参数错误！');
            }
        } else {
            $this->error('参数错误！');
        }

    }


}